package com.huabo.fxgl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbfk.util.JsonBean;
import com.huabo.fxgl.entity.Organization;
import com.huabo.fxgl.entity.Risk;
import com.huabo.fxgl.entity.Riskcategory;
import com.huabo.fxgl.service.impl.RiskServiceImpl;
import com.huabo.fxgl.service.impl.RiskcategoryServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2022-08-01
 */
@RestController
@RequestMapping("/risk")
@Api(tags = "风险管控-风险识别-风险数据库")
public class RiskController {
    @Autowired
    private RiskcategoryServiceImpl riskcategoryService;
    @Autowired
    private RiskServiceImpl riskService;

    @RequestMapping(value = "/risk_fxsjk_list")
    @ApiOperation("风险管控 - 风险识别 - 风险数据库 - 列表 /risk/risk_fxsjk_list")
    public JsonBean risk_fxsjk_list(/*HttpServletRequest request, TblRisk risk*/
            Risk risk,
            @ApiParam(name = "pageNumber", value = "pageNumber") @RequestParam(defaultValue = "1") Integer pageNumber,
            @ApiParam(name = "limit", value = "limit") @RequestParam(defaultValue = "15") Integer limit,
            @ApiParam(name = "choiceSearch", value = "choiceSearch") String choiceSearch,
            @ApiParam(name = "riskcatid", value = "riskcatid") String riskcatid,
            @ApiParam(name = "orgid", value = "orgid") Integer orgid,
            @ApiParam(name = "organization", value = "organization") Organization organization) {

        // 如果客户端未提供riskcatid，那么使用orgid查询对应的风险类别
        if (StringUtils.isEmpty(riskcatid)) {
            if (organization==null || organization.getOrgid()==null) {
                return new JsonBean(400, "未提供orgid参数，无法继续", null);
            }
            Riskcategory cat = riskcategoryService.findQYFXByOrgid(organization.getOrgid().toString(), Riskcategory.FXSJK);
            if (cat != null) {
                riskcatid = cat.getRiskcatid().toString();
            }
        }

        IPage page = new Page(pageNumber, limit);//分页设置
        IPage pageBean = riskService.getRiskList(page, riskcatid, risk);

        Riskcategory riskcategory = riskcategoryService.getById(riskcatid);
        String riskcategoryName = null;
        if (riskcategory != null) {
            riskcategoryName = riskcategoryService.findRiskcatByName(riskcategory);
        }

        if(choiceSearch == null || "".equals(choiceSearch)) {
            choiceSearch = "hide";
        }
        Map result = new HashMap();
        result.put("choiceSearch", choiceSearch );
        result.put("riskcategoryName", riskcategoryName);
        result.put("riskcategory", riskcategory);
        result.put("riskcatid", riskcatid);
        result.put("orgid", orgid);
        result.put("pageBean", pageBean);
        //mv.addObject("pager", pager);
        result.put("risk", risk);
        return new JsonBean(200, "success", result);

    }




}
